package com.ccbft.msgcenter.mc.filter.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ccbft.msgcenter.mc.filter.entity.ChainInstance;
import com.ccbft.msgcenter.mc.filter.entity.ChainNode;
import com.ccbft.msgcenter.mc.filter.service.IChainInstanceService;
import com.ccbft.msgcenter.mc.filter.vo.ChainInstanceVo;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author frank
 * @since 2019-07-20
 */
@RestController
@RequestMapping("/filter/chains")
public class ChainInstanceController {
	@Autowired
	private IChainInstanceService chainInstanceService;
	
	
	@RequestMapping(method=RequestMethod.POST, consumes = "application/json")
	public boolean add(@RequestBody ChainInstance chainInstance) {
		return chainInstanceService.save(chainInstance);
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	public boolean deleteById(@PathVariable("id") Long id) {
		return chainInstanceService.removeById(id);
	}
	
	@RequestMapping(method=RequestMethod.GET )
	public List<ChainInstance> queryAll(){
		return chainInstanceService.list();
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public ChainInstanceVo queryById(@PathVariable("id") Long id) {
		ChainInstance chainInstance = chainInstanceService.getById(id);
		if(chainInstance==null)
			return null;
		else {
			ChainInstanceVo outVo = new ChainInstanceVo(chainInstance);
			List<ChainNode> chainNodeList = chainInstanceService.queryChainNodeListByInstanceId(id);
			outVo.setChainNodeList(chainNodeList);
			return outVo;
		}
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.PUT)
	public boolean updateById(@PathVariable("id") Long id,@RequestBody ChainInstance entity) {
		entity.setId(id);
		return chainInstanceService.updateById(entity);
	}	
	
}
